<template>
    <div id="code"></div>
</template>

<script>
    import Vue from 'vue'
    import VueResource from 'vue-resource'

    Vue.use(VueResource)

    function GetRequest(strName) {
        var strHref = window.location.href
        var intPos = strHref.indexOf("?");
        var strRight = strHref.substr(intPos + 1);
        var arrTmp = strRight.split("&");
        for (var i = 0; i < arrTmp.length; i++) {
            var arrTemp = arrTmp[i].split("=")
            if (arrTemp[0].toUpperCase() == strName.toUpperCase()) {
                return arrTemp[1]
            }
        }
        return '';
    }

    export default {
        mounted(){
            var code = GetRequest("code")
            var eid = localStorage.getItem("eid")
            if (code && eid) {
                const obj = {code: code, eid: eid}
                this.$http.post('/oauth/v1/wechat/userinfo/', obj)
                    .then((res) => {
                        if (res.data.code == '1') {
                            localStorage.setItem("Token", res.data.Token)
                            localStorage.setItem("openid", res.data.openid)
                            console.log(res.data)
                            window.location.href = 'http://www.nowdone.com.cn/'
                        } else {
                            console.error('/error')
                            this.$router.push('/error')
                        }
                    }, (err) => {
                        console.error(err)
                    })
            } else {
                console.error('error')
                this.$router.push('/error')
            }
        }
    }
</script>

<style lang="less"></style>
